home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d941.lha / Guide2Doc / Guide2Doc.c < prev    next >
C/C++ Source or Header  |  1993-12-20  |  11KB  |  324 lines

  1. ///___________"Guide2Doc 1.0 (02-Nov-1993) FreeWare - © Koessi"___________START
  2.  
  3. /* Guide2Doc 1.0 (02-Nov-1993) FreeWare - © Koessi
  4.    based on "ConvertGuide2Doc" by Jorrit Tyberghein
  5.    
  6.    Convert AmigaGuide® file to a normal document
  7.    without any "@{xxx}"'s, but full ANSI-support!
  8.  
  9.    CLI-ONLY-Usage: INPUTFILE/A,PAGELENGTH/N,FIRSTPAGE/N
  10.  
  11.    Optional number PAGELENGTH will force chapters
  12.    to start on next page if less than ten lines are
  13.    left on the actual page and a table of contents
  14.    will be generated.
  15.  
  16.    Additional number FIRSTPAGE will start counting
  17.    pages from this and put pagenumbers centered in
  18.    an added footline (2 lines of pagelength used).
  19.  
  20.    Output goes to StandardOut, so you may redirect
  21.    it, eg. ">prt:", or read the guide in CLI.
  22.  */
  23.  
  24. ///_________________________________"1.0"__________________________________ENDE
  25.  
  26. ///_________________________"#include <stdlib.h>"_________________________START
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. char version[] = "$VER: Guide2Doc 1.0 (02-Nov-1993) FreeWare - © Koessi";
  33. char pagestr[] = "\n\t\t\t\t--- %ld ---\n";
  34. char *nullstr  = "";
  35.  
  36. char helpstr[] = "\n\t\t\x9B0;32m%s\x9B0;33m\n"
  37.  
  38.              "\t\tbased on \"ConvertGuide2Doc\" by Jorrit Tyberghein\n\n"
  39.              "\t\tConvert AmigaGuide® file to a normal document -\n"
  40.              "\t\twithout any \"@{xxx}\"'s, but full ANSI-support!\x9B0m\n\n"
  41.  
  42.   "CLI-ONLY-Usage:\t\x9B1;32m%s INPUTFILE/A,PAGELENGTH/N,FIRSTPAGE/N\x9B0;33m\n\n"
  43.  
  44.              "\t\tOptional number \x9B0;32mPAGELENGTH\x9B0;33m will force chapters\n"
  45.              "\t\tto start on next page if less than ten lines are\n"
  46.              "\t\tleft on the actual page and a table of contents\n"
  47.              "\t\twill be generated.\n\n"
  48.  
  49.              "\t\tAdditional number \x9B0;32mFIRSTPAGE\x9B0;33m will start counting\n"
  50.              "\t\tpages from this and put pagenumbers centered in\n"
  51.              "\t\tan added footline (2 lines of pagelength used).\n\n"
  52.  
  53.              "\t\tOutput goes to \x9B0;32mStandardOut\x9B0;33m, so you may redirect\n"
  54.              "\t\tit, eg. \">prt:\", or read the guide in CLI.\x9B0m\n";
  55.  
  56. #define STAT_NO (0)
  57. #define STAT_BD (1<<1)
  58. #define STAT_IT (1<<3)
  59. #define STAT_UL (1<<4)
  60. #define ERRORRR -1
  61. #define BUFSIZE 4096
  62. #define ANSI_NO(s) { *((int *)s) = 0x9B306D00; s += 3; }
  63. #define ANSI_BD(s) { *((int *)s) = 0x9B316D00; s += 3; }
  64. #define ANSI_IT(s) { *((int *)s) = 0x9B336D00; s += 3; }
  65. #define ANSI_UL(s) { *((int *)s) = 0x9B346D00; s += 3; }
  66.  
  67. ///_________________________"#include <stdlib.h>"__________________________ENDE
  68.  
  69. int 
  70. main(int argc, char *argv[])
  71. {
  72.   if (argc < 2 || *argv[1] == '?')
  73.   {
  74.     printf(helpstr, &version[6], argv[0]); return;
  75.   }
  76.  
  77.   FILE *fp;
  78.   char *buf = NULL, *buf2 = NULL, *buf3 = NULL, *p, *p2;
  79.   int state = ERRORRR, ansi = STAT_NO, line = 1, maxline = 100000, page = 1, nodes = 0, t;
  80.  
  81.   if ((buf = malloc(BUFSIZE)) && (buf2 = malloc(BUFSIZE)) && (buf3 = malloc(32)))
  82.   {
  83.     if (fp = fopen(argv[1], "r"))
  84.     {
  85.       if (fgets(buf, BUFSIZE - 1, fp) && !strnicmp(buf, "@Database", 9))
  86.       {
  87.  
  88. ///______________________"________@Database"______________________________START
  89.  
  90.         buf[strlen(buf) - 1] = '\0';
  91.         *buf3 = '\0';
  92.         p     = &buf[9];
  93.         p2    = buf2;
  94.         ANSI_BD(p2);
  95.  
  96.         state = 1;
  97.  
  98.         while (*p)
  99.         {
  100.           switch (state)
  101.           {
  102.             case 1:             /* Waiting for first/third double quote */
  103.               if (*p == '"')
  104.               {
  105.                 state = 2;
  106.                 p2 = &(buf2[3]);
  107.                 t  = (72 - strlen(p)) / 2;
  108.                 while (t-- > 0) *p2++ = ' ';
  109.               }
  110.               else 
  111.                 *p2++ = *p;
  112.               break;
  113.             case 2:             /* Waiting for second/fourth double quote */
  114.               if (*p == '"')
  115.                 state = 1;
  116.               else
  117.                 *p2++ = *p;
  118.             default:
  119.               break;
  120.           }
  121.           p++;
  122.         }
  123.         ANSI_NO(p2); *p2 = '\0';
  124.         puts(buf2);
  125.         ++line;
  126.  
  127. ///______________________________"@Database"_______________________________ENDE
  128.  
  129.         if (argc > 2) 
  130.         {
  131.           maxline = atoi(argv[2]);
  132.           if (argc > 3)
  133.           {
  134.             page = atoi(argv[3]);
  135.             maxline -= 2;
  136.           }
  137.         }
  138.  
  139.         while (fgets(buf, BUFSIZE - 1, fp))                           /* LOOP STARTS HERE */
  140.         {
  141.           buf[strlen(buf) - 1] = 0;
  142.           p = buf;
  143.  
  144.           if (*((short *)p) == '##') continue;                        /* skip commentline */
  145.  
  146.           if (*p == '@')                    /* '@' - found that special token at start of line */
  147.           {
  148.  
  149. ///__________________"_____________@Author"_______________________________START
  150.  
  151.             if (!strnicmp(p + 1, "Author", 6))
  152.             {
  153.               if (line < maxline) 
  154.               { 
  155.                 *++p = 'A';
  156.                 puts(p); ++line;
  157.               }
  158.               continue;
  159.             }
  160.  
  161. ///_______________________________"@Author"________________________________ENDE
  162.  
  163. ///___________________"_____________@Node"________________________________START
  164.  
  165.             if (!strnicmp(p + 1, "Node", 4))
  166.             {
  167.               if (line >= (maxline - 10))
  168.               {
  169.                 while (line++ <= maxline) puts(nullstr);
  170.                 if (argc > 3) printf(pagestr, page);
  171.                 line = 1;
  172.                 ++page;
  173.               }
  174.               ++nodes;
  175.  
  176.               p2    = buf2;
  177.               ANSI_BD(p2);
  178.  
  179.               state = 1;
  180.               p    += 5;
  181.  
  182.               while (*p)
  183.               {
  184.                 switch (state)
  185.                 {
  186.                 case 1:             /* Waiting for first/third double quote */
  187.                   if (*p == '"')
  188.                   {
  189.                     state = 2;
  190.                     p2 = &(buf2[3]);
  191.                   }
  192.                   else
  193.                     *p2++ = *p;
  194.                   break;
  195.                 case 2:             /* Waiting for second/fourth double quote */
  196.                   if (*p == '"')
  197.                     state = 1;
  198.                   else
  199.                     *p2++ = *p;
  200.                   break;
  201.                 }
  202.                 p++;
  203.               }
  204.               ANSI_NO(p2);
  205.               puts(buf2);
  206.               ++line;
  207.  
  208.               if ((argc > 2) && (buf3 = realloc(buf3, nodes * 80)))  /* make contents entry */
  209.               {
  210.                 p    = *buf3 ? &buf3[strlen(buf3)] : buf3;
  211.                 p2   = &buf2[3];
  212.                 *p++ = ' '; 
  213.                 *p++ = ' ';
  214.                 t    = 0;
  215.                 while (*p2 != (char)0x9B && t++ < 60) *p++ = *p2++;
  216.                 while (t++ < 65)                      *p++ = '.';
  217.                 sprintf(p, "%3ld\n", page);
  218.               }
  219.               continue;
  220.             }
  221.  
  222. ///________________________________"Node"__________________________________ENDE
  223.  
  224.             if (p[1] != '{') continue;      /* ignore everything but links and ansicodes */
  225.           }
  226.  
  227. ///_____"______________'@' - found that special token"____________________START
  228.  
  229.           if (p2 = strstr(p, "@{"))                         /* find that special token */
  230.           {
  231.             state = 0;
  232.             p2    = buf2;
  233.  
  234.             while (*p)
  235.             {
  236.               switch (state)
  237.               {
  238.               case 0:                                       /* Initial state */
  239.                 if (*((short *)p) == '@{')                   /* found that special token */
  240.                 {
  241.                   switch(p[2])
  242.                   {
  243.                     case 'b': ansi |= STAT_BD; break;       /* start bold */
  244.                     case 'i': ansi |= STAT_IT; break;       /* start italic */
  245.                     case 'u':
  246.                       switch(p[3])
  247.                       {
  248.                         case '}': ansi |=  STAT_UL;      break;  /* start underlined */
  249.                         case 'b': ansi &= ~STAT_BD; ++p; break;  /* stop bold */
  250.                         case 'i': ansi &= ~STAT_IT; ++p; break;  /* stop italic */
  251.                         case 'u': ansi &= ~STAT_UL; ++p; break;  /* stop underline */
  252.                         default:  state =  ERRORRR;      break;  /* unknown token - error */
  253.                       }
  254.                       break;
  255.                     default: ++p; state = 1; break;         /* this must be a link ! */
  256.                   }
  257.                   if (0 == state)                           /* (re)set ansi state */
  258.                   {
  259.                     p += 3;             ANSI_NO(p2);
  260.                     if (ansi & STAT_BD) ANSI_BD(p2);
  261.                     if (ansi & STAT_IT) ANSI_IT(p2);
  262.                     if (ansi & STAT_UL) ANSI_UL(p2);
  263.                   }
  264.                 }
  265.                 else *p2++ = *p;
  266.                 break;
  267.               case 1:                                       /* Waiting for first double quote */
  268.                 if (*p == '"') { ANSI_IT(p2); state = 2; }
  269.                 break;
  270.               case 2:                                       /* Waiting for second double quote */
  271.                 if (*p == '"') { ANSI_NO(p2); state = 3; }
  272.                 else
  273.                   *p2++ = *p;
  274.                 break;
  275.               case 3:                                       /* Waiting for '}' */
  276.                 if (*p == '}') state = 0;
  277.                 break;
  278.               }
  279.               p++;                                          /* get next char from inputlinebuffer */
  280.             }                                               /* this inputline is done */
  281.             *p2 = 0;                                        /* mark end of outputbuffer */
  282.             p   = buf2;
  283.           }
  284.  
  285. ///___________________"'@' - found that special token"_____________________ENDE
  286.  
  287.           if (ERRORRR == state) break;                      /* after-each-line-error-check !!! */
  288.  
  289.           puts(p);                                          /* printout line */
  290.           if (++line > maxline)                             /* inc counters */
  291.           { 
  292.             if (argc > 3) printf(pagestr, page);
  293.             line = 1; 
  294.             ++page; 
  295.           }
  296.         }
  297.         if (ERRORRR == state)                                 /* final error check !!! */
  298.           printf("Error in Database - Line %ld\n", line);
  299.         else
  300.         {
  301.           if ((argc > 2) && *buf3)
  302.           {
  303.             puts(nullstr); ++line;
  304.             t = (nodes > maxline) ? (nodes % maxline) : (maxline - nodes);
  305.  
  306.             if (t > (maxline - line - 2))
  307.             {
  308.               while (line++ < maxline) puts(nullstr);
  309.               if (argc > 3) printf(pagestr, page);
  310.             }
  311.             printf("\x9B1mCONTENTS\x9B0m\n\n%s", buf3);
  312.           }
  313.           printf("\n  %s converted by\n  %s\n", argv[1], &version[6]);
  314.         }
  315.       }
  316.       fclose(fp);
  317.     }
  318.     else puts("Error opening file!");
  319.   }
  320.   if (buf)  free(buf);
  321.   if (buf2) free(buf2);
  322.   if (buf3) free(buf3);
  323. }
  324.